'''
目标函数：
    min　z=|x1|+2*|x2|+3*|x3|+4*|x4|
约束条件：
    x1-x2-x3+x4=0
    x1-x2+x3-3*x4=1
    x1-x2+2*x3+3*x4=-1/2

这类问题的解法是取ui=(|xi|+xi)/2  vi=(|xi|-xi)/2 代入即可
即得到：
    min z=u1+2*u2+3*u3+4*u4+v1+2*v2+3*v3+4*v4
    u1-u2-u3+u4-v1+v2+v3-v4=0
    u1-u1+u3-3*u4-v1+v2-v3+3*v4=1
    u1-u2-2*u3+3*u4-v1+v2+2*v3-3*v4=-1/2
    ui,vi>=0 (i=1,2,3,4)
'''
import numpy as np
from scipy.optimize import linprog

c = np.array([1, 2, 3, 4, 1, 2, 3, 4])
A_eq = np.array([[1, -1, -1, 1, -1, 1, 1, -1], [1, -1, 1, -3, -1, 1, -1, 3], [1, -1, -2, 3, -1, 1, 2, -3]])
b_eq = np.array([0, 1, -1 / 2])
bounds = (0, None)
res = linprog(c, A_eq=A_eq, b_eq=b_eq, bounds=(bounds, bounds, bounds, bounds, bounds, bounds, bounds, bounds))
print(res)
'''
     fun: 1.2499999999999998
 message: 'Optimization terminated successfully.'
     nit: 2
   slack: array([], dtype=float64)
  status: 0
 success: True
       x: array([0.25, 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.25])
'''
